package com.nowcoder.community.config;

import com.nowcoder.community.interceptor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: shuhuang
 * @description: 加载自定义的拦截器，并进行拦截配置
 * @Date: 2022/10/12 16:56
 * @Version java version 1.8
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private TestInterceptor testInterceptor;
    @Autowired
    private LoginTicketInterceptor loginTicketInterceptor;
    //    @Autowired
//    private LoginRequiredInterceptor loginRequiredInterceptor;
    @Autowired
    private IndexViewInterceptor indexViewInterceptor;
    @Autowired
    private MessageInterceptor messageInterceptor;
    @Autowired
    private DataInterceptor dataInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*registry.addInterceptor(testInterceptor)
                .addPathPatterns("/register", "/login")
                .excludePathPatterns("/css/*", "/img/*", "/js/*");*/
        //不加拦截路径就默认为所有
        //使用order()方法来定义多个拦截器的执行顺序，默认为0，值越小优先级越高
        //fixme 这里拦截请求有问题，好多请求重复查询几遍，待修复
        registry.addInterceptor(loginTicketInterceptor).order(0)
                .excludePathPatterns("/static/**", "/kaptcha");
        /*registry.addInterceptor(loginRequiredInterceptor).order(1)
                .excludePathPatterns("/static/**");*/
        registry.addInterceptor(indexViewInterceptor).order(2)
                .addPathPatterns("/")
                .excludePathPatterns("/static/**");
        registry.addInterceptor(messageInterceptor)
                .excludePathPatterns("/static/**");
        registry.addInterceptor(dataInterceptor)
                .excludePathPatterns("/static/**");
    }


  /*  @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/500")
                .setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)
                .setViewName("/site/error/500.html");
        registry.addViewController("/404")
                .setStatusCode(HttpStatus.NOT_FOUND)
                .setViewName("/site/error/405.html");
    }*/
}
